package com.cari.oa.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.cari.oa.domain.OperationLogEntity;
import com.cari.oa.domain.SysUserEntity;
import com.cari.oa.service.OperationLogService;
import com.cari.oa.service.SysUserService;
import com.cari.oa.util.Contants;
import com.cari.oa.util.DateUtils;
import com.cari.oa.util.Utils;

@Controller
@SessionAttributes("username")
public class OperationLogController {
	@Resource
	private OperationLogService operationLogService;
	@Resource
	private SysUserService sysUserService;
	
	public static final String MODULE_NAME = "NEWS_MANAGE";
	
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }
    
    @RequestMapping("/req/operationlog/list.do")
    public String list(String title, String content, String operationUserEn,
    		String starttime,
			String endtime,
    		Integer curPage, Integer pageSize, ModelMap modelMap) {
        if(curPage == null) curPage = 1;
        long totalRecords = operationLogService.getCount(title, content, operationUserEn,
        		DateUtils.str2Timestamp(starttime, DateUtils.NORMAL_FORMAT), 
        		DateUtils.str2Timestamp(endtime, DateUtils.NORMAL_FORMAT));
        long totalPage = 0;
        if(pageSize == null)
        	totalPage = totalRecords / Contants.PAGE_SIZE + (totalRecords % Contants.PAGE_SIZE != 0 ? 1 : 0);
        else
        	totalPage = totalRecords / pageSize + (totalRecords % pageSize != 0 ? 1 : 0);
        List<OperationLogEntity> operationLogList = null;
        if(pageSize == null)
        	operationLogList = operationLogService.getPage(title, content, operationUserEn, 
        		DateUtils.str2Timestamp(starttime, DateUtils.NORMAL_FORMAT), 
        		DateUtils.str2Timestamp(endtime, DateUtils.NORMAL_FORMAT),
        		curPage, Contants.PAGE_SIZE);
        else
        	operationLogList = operationLogService.getPage(title, content,operationUserEn, 
            		DateUtils.str2Timestamp(starttime, DateUtils.NORMAL_FORMAT), 
            		DateUtils.str2Timestamp(endtime, DateUtils.NORMAL_FORMAT),
            		curPage, pageSize);
        List<Map<String, String>> operationUsers = operationLogService.getOperationUser();
        
        modelMap.addAttribute("operationLogList", operationLogList);
        modelMap.addAttribute("operationUsers", operationUsers);
        modelMap.addAttribute("curPage", curPage);
        modelMap.addAttribute("totalPage", totalPage);
        modelMap.addAttribute("totalRecords", totalRecords);
        modelMap.addAttribute("starttime", starttime);
        modelMap.addAttribute("endtime", endtime);

        modelMap.addAttribute("title", title);
        modelMap.addAttribute("content", content);
        modelMap.addAttribute("operationUserEn", operationUserEn);
        modelMap.addAttribute("pageSize", pageSize);
        modelMap.addAttribute("content", content);

        return "req/operationlog/list";
    }
    
    @RequestMapping("/req/operationlog/userlist.do")
    public String userlist(@ModelAttribute("username") String username, String title, String content,
    		String starttime,
			String endtime,
    		Integer curPage,Integer pageSize, ModelMap modelMap) {
        if(curPage == null) curPage = 1;
        long totalRecords = operationLogService.getCount(title, content, username,
        		DateUtils.str2Timestamp(starttime, DateUtils.NORMAL_FORMAT), 
        		DateUtils.str2Timestamp(endtime, DateUtils.NORMAL_FORMAT));
        long totalPage = 0;
        if(pageSize == null)
        	totalPage = totalRecords / Contants.PAGE_SIZE + (totalRecords % Contants.PAGE_SIZE != 0 ? 1 : 0);
        else
        	totalPage = totalRecords / pageSize + (totalRecords % pageSize != 0 ? 1 : 0);
        List<OperationLogEntity> operationLogList = null;
        if(pageSize == null)
        	operationLogList = operationLogService.getPage(title, content, username, 
        		DateUtils.str2Timestamp(starttime, DateUtils.NORMAL_FORMAT), 
        		DateUtils.str2Timestamp(endtime, DateUtils.NORMAL_FORMAT),
        		curPage, Contants.PAGE_SIZE);
        else
        	operationLogList = operationLogService.getPage(title, content, username, 
            		DateUtils.str2Timestamp(starttime, DateUtils.NORMAL_FORMAT), 
            		DateUtils.str2Timestamp(endtime, DateUtils.NORMAL_FORMAT),
            		curPage, pageSize);
        
        modelMap.addAttribute("operationLogList", operationLogList);
        modelMap.addAttribute("curPage", curPage);
        modelMap.addAttribute("totalPage", totalPage);
        modelMap.addAttribute("totalRecords", totalRecords);
        modelMap.addAttribute("starttime", starttime);
        modelMap.addAttribute("endtime", endtime);

        modelMap.addAttribute("title", title);
        modelMap.addAttribute("content", content);
        modelMap.addAttribute("pageSize", pageSize);

        return "req/operationlog/userlist";
    }
    
    @RequestMapping(value = "/req/operationlog/insert.do", method = RequestMethod.GET)
    public String enterInsert(@ModelAttribute("username") String username, String qs, ModelMap modelMap) {
    	SysUserEntity user = sysUserService.getByEnname(username);
        modelMap.addAttribute("user", user);
        modelMap.addAttribute("qs", qs);
        return "req/operationlog/insert";
    }

    @RequestMapping(value = "/req/operationlog/view.do", method = RequestMethod.GET)
    public String enterView(@ModelAttribute("username") String username, String id, ModelMap modelMap) {
        OperationLogEntity operationLog = operationLogService.getById(id);
        SysUserEntity user = sysUserService.getByEnname(username);
        modelMap.addAttribute("user", user);
        modelMap.addAttribute("operationLog", operationLog);
        return "req/operationlog/view";
    }
    
    @RequestMapping(value = "/req/operationlog/edit.do", method = RequestMethod.GET)
    public String enterEdit(String id, String qs, ModelMap modelMap) {
    	OperationLogEntity operationLog = operationLogService.getById(id);
    	modelMap.addAttribute("operationLog", operationLog);
        modelMap.addAttribute("qs", qs);
        return "req/operationlog/edit";
    }
    
    @RequestMapping(value = "/req/operationlog/insert.do", method = RequestMethod.POST)
    @ResponseBody
    public String postInsert(@ModelAttribute("username") String username, 
    		@Valid OperationLogEntity operationLogEntity, BindingResult result) {
        if(result.hasFieldErrors())
            return Utils.errorJsonResp(result.getFieldError().getDefaultMessage());

        operationLogService.insert(operationLogEntity);
        
        SysOperLogController.addOperLog(username, MODULE_NAME, "新增运维记录" + operationLogEntity.getTitle());
        return Utils.normalJsonResp();
    }
    
    @RequestMapping(value = "/req/operationlog/edit.do", method = RequestMethod.POST)
    @ResponseBody
    public String postEdit(@ModelAttribute("username") String username, 
    		@Valid OperationLogEntity operationLogEntity, BindingResult result) {
        if(result.hasFieldErrors())
            return Utils.errorJsonResp(result.getFieldError().getDefaultMessage());

        Date startTime = operationLogEntity.getDoTime();
        Date endTime = operationLogEntity.getEndTime();
        if(endTime.before(startTime))
        	return Utils.errorJsonResp("运维结束时间必须在开始时间以后");
        operationLogService.update(operationLogEntity);
        
        SysOperLogController.addOperLog(username, MODULE_NAME, "修改运维记录" + operationLogEntity.getTitle());
        return Utils.normalJsonResp();
    }
}
